package cn.yunhe.pojo;

import cn.yunhe.exception.OutOfStockException;
import cn.yunhe.model.Customer;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class LogAspect2Refrigerator {
    Map<String,Customer> customerMap=new HashMap<>();
    static int quantity=9;
    int maxCount2Curr=3;
    public void before111(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        Object[] objects=joinPoint.getArgs();
        Customer customer= (Customer) objects[0];
        int count= (int) objects[1];

        if ("saleRefrigerator".equals(methodName)){
            System.out.println("[前置通知:欢迎"+customer.getName()+"购买电冰箱，打算购买"+count+" 台，实际库存"+quantity+"]");
        }
    }

    public void afterReturning111(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        Object[] objects=joinPoint.getArgs();
        Customer customer= (Customer) objects[0];
        int count= (int) objects[1];

        if ("saleRefrigerator".equals(methodName)){
            System.out.println("[后置成功通知:恭喜"+customer.getName()+"成功购买电冰箱，成功购买"+count+" 台]");
        }
    }

    public void afterThrowing111(JoinPoint joinPoint, OutOfStockException e){
        String methodName=joinPoint.getSignature().getName();
        Object[] objects=joinPoint.getArgs();
        Customer customer= (Customer) objects[0];
        int count= (int) objects[1];

        if ("saleRefrigerator".equals(methodName)){
            System.out.println("[异常通知:"+customer.getName()+"购买电冰箱时，购买失败: "+e.getMessage()+"]");
        }
    }

    public Object around111(ProceedingJoinPoint proceedingJoinPoint){
        /*int maxCount2Cus=3;*/
        String methodName=proceedingJoinPoint.getSignature().getName();
        Object[] objects=proceedingJoinPoint.getArgs();

        System.out.println("[环绕通知开始:methodName==="+methodName+"   getArgs[0]==="+objects[0]+"    getArgs[1]===="+objects[1]+"，实际库存"+quantity+"]");
        Customer customer= (Customer) objects[0];
        int count= (int) objects[1];
        boolean flag2Key=customerMap.containsKey(customer.getName());
        boolean flag2Vlaue=customerMap.containsValue(customer);


        try {
            System.out.println("~~~~~~~~~~~~~~~~~");
            customerMap(customerMap);
            System.out.println("~~~~~~~~~~~~~~~~~");
            if (flag2Key&&flag2Vlaue) {
                System.out.println("[11环绕通知提示:"+customer.getName() + "您已购买冰箱,每人限购 "+3+" 台,所以无法再次购，实际库存"+quantity+"]");
                return null;
            }else{
                maxCount2Curr-=count;
                if(maxCount2Curr==0){
                    customerMap.put(customer.getName(),customer);
                }else if(maxCount2Curr<0) {
                    maxCount2Curr+=count;
                    System.out.println("[22环绕通知提示:"+customer.getName() + "您已购买冰箱,每人限购 "+3+" 台，还可以购买"+maxCount2Curr+" 台，实际库存"+quantity+"]");
                    return null;
                }
                return proceedingJoinPoint.proceed();
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }finally {
            System.out.println("[环绕通知结束，还可以购买"+maxCount2Curr+" 台，实际库存"+quantity+"]");
        }
    }

    public void customerMap(Map<String,Customer> customerMap){
        Set<Map.Entry<String, Customer>> entrySet2Customer = customerMap.entrySet();
        Iterator<Map.Entry<String, Customer>> iterator = entrySet2Customer.iterator();
        System.out.println("~~~~~[环绕通知]已成功购买的顾客列表START");
        while (iterator.hasNext()) {
            Map.Entry<String, Customer> customerEntry = iterator.next();
            System.out.println(customerEntry.getKey() + "===" + customerEntry.getValue());
        }
        System.out.println("~~~~~[环绕通知]已成功购买的顾客列表END");
    }

    public void after111(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        Object[] objects=joinPoint.getArgs();
        //System.out.println("[最终通知:methodName==="+methodName+"   getArgs[0]==="+objects[0]+"    getArgs[0]===="+objects[1]);
        Customer customer= (Customer) objects[0];
        int count= (int) objects[1];
        quantity-=count;
        if (quantity<0){
            quantity+=count;
            System.out.println("[最终通知:"+customer.getName()+" 需要购买冰箱 "+count+" 台，超出实际剩余库存 "+(quantity)+" 台，无法完成购买]");
        }else if (quantity>=0&&count>3){
            quantity+=count;
            System.out.println("[最终通知:"+customer.getName()+" 无法购买冰箱 "+count+" 台，超出限购台数，当前实际剩余库存 "+(quantity)+" 台]");
        }else {
            System.out.println("[最终通知:"+customer.getName()+" 完成购买冰箱 "+count+" 台，当前实际剩余库存 "+(quantity)+" 台]");
        }
    }
}
